% z = POLYPLUGIN(p.x) Evaluate the polynomial p(x) at x
%
%   Inputs:
%
%       p    The polynomial to be evaluated
%       x    The argument to the polynomial
%
%   Outputs:
%
%       z    The polynomial p(x) evaluated at x

function z = polyPlugIn(p,x)

    % If the polynomial is zero, return zero
    if polyIsZero(p)
        z = -1;
        return;
    end

    % Add the constant term
    z = p(1);

    % If the polynomial is only a constant, return that constant
    if length(p) == 1
        return;
    end

    % Evaluate p(x)
    for i = 2 : length(p)

        if ~isZero(p(i))

            term = elementPow(x,i-1);
            term = elementMult(term,p(i));
            z = elementAdd(z,term);

        end
    end
end
